package Scanner;

import java.util.List;

import nfa_generator.NFAGenerator;
import nfa_generator.NFAGraph;
import table_walker.TableWalker;
import table_walker.Token;
import DFA.DFA;
import ast_generator.ASTTree;
import ast_generator.TreeGenerator;
import ast_generator.*;

public class Scanner implements ScannerInterface {
	private final DFA dfa;
	
	public Scanner (String lexSpecFile) {
		List<ASTTree> astTreeList = TreeGenerator.makeTree(lexSpecFile);
		NFAGenerator nfaGenerator = new NFAGenerator(astTreeList);
		NFAGraph nfaGraph = nfaGenerator.generateNFA();
		this.dfa = new DFA(nfaGraph);
	}
	
	public List<Token> scan (String scriptFile) throws Exception {
		TableWalker tWalker = new TableWalker(scriptFile,dfa);
		List<Token> output = tWalker.walk();
		for (Token token : output) {
			if (	token.getName().equals("begin") || 
					token.getName().equals("end") || 
					token.getName().equals("replace") ||
					token.getName().equals("with") ||
					token.getName().equals("in") || 
					token.getName().equals("print") ||
					token.getName().equals("find") ||
					token.getName().equals("diff") ||
					token.getName().equals("union") || 
					token.getName().equals("inters") )
				token.setType("$KEYWORD");
			/*if (	token.getType().equals("$ID") || 
					token.getType().equals("$ID2") || 
					token.getType().equals("$ID3") ||
					token.getType().equals("$ID4") ||
					token.getType().equals("$ID5") || 
					token.getType().equals("$ID6") ||
					token.getType().equals("$ID8") ||
					token.getType().equals("$ID9") ||
					token.getType().equals("$ID7") )
				token.setType("ID");*/
		}
		return output;
	}
}
